<template>
    <div id="background">
        <el-container>
            <el-header id="editPhotoHeadPosition">
                <el-row type="flex" style="height:80%;display:flex;align-items: flex-end">
                    <el-col :xs="2" :sm="2" :md="2" :offset="1"  class="imgCol">
                        <img v-bind:src="backIcon" class="backSize" @click="back"/>
                    </el-col>
                    <el-col  :xs="6" :sm="6" :md="6" :offset="6" class="imgCol"> 
                        <span class="titleSize" >编辑照片</span>
                    </el-col>
                    <el-col :xs="3" :sm="6" :md="6" :offset="5" class="imgCol">
                        <span class="titleSize" @click="editPhoto">保存</span>
                    </el-col>
                </el-row>
            </el-header>
            <el-main  id="editPhotoMainPositon" class="imgCol">
                        <el-upload
                            class="avatar-uploader"
                            action="https://jsonplaceholder.typicode.com/posts/"
                            :http-request="handleAvatarSuccess"
                            :show-file-list="false"
                            >
                            <img v-if="head" :src="head" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
            </el-main>
            <el-footer id="editPhotoFootPositon">
               
                <el-row type="flex" justify="center">
                    <button class="btnPhoto" @click="galleryImg"><i class="el-icon-picture-outline" style="padding-right:20px"></i>从相册选取</button>
                    <!-- <van-uploader :after-read="afterRead" >
                        <button class="btnPhoto" ><i class="el-icon-picture-outline" style="padding-right:20px"></i>从相册选取</button>
                    </van-uploader> -->
                
                </el-row>
                <el-row type="flex" justify="center" style="margin-top:20px">
                    <button class="btnPhoto" @click="getImage"><i class="el-icon-camera" style="padding-right:20px"></i>立刻拍一张</button>
                </el-row>
            </el-footer>
        </el-container>
    </div>
</template>
<script>
import backIcon from '../../assets/img/mine/geRenZhongXin/back.png'
import userPhoto from '../../assets/img/mine/geRenZhuYe/test.jpg'
import xiangCeIcon from '../../assets/img/mine/edit/xiangCe.png'
import paiZhao from '../../assets/img/mine/edit/paiZhao.png'
import { Toast } from 'vant';
export default {
    data(){
        return{
            backIcon:backIcon,
            normalHeight:document.documentElement.clientHeight,
            head:'',
            headFile:'',
        }
    },
    
    mounted(){
       var normalHeight=sessionStorage.getItem('normalHeight');

        var bg = document.getElementById("background");
       // normalHeight=document.documentElement.clientHeight;
        console.log("height:"+normalHeight);
        bg.style.height = normalHeight+"px";
        bg.style.backgroundColor = "#FFFFFF"
        bg.style.margin = "0px";

        var head=document.getElementById("editPhotoHeadPosition");
        var headHeight=normalHeight/30 *4 ;
        console.log("headHeight:"+headHeight);
        head.style.height=headHeight+"px";

        var main=document.getElementById("editPhotoMainPositon");
        var mainHeight=normalHeight/30 *15;
        console.log("mainHeight:"+mainHeight);
        main.style.height=mainHeight+"px";

        var foot=document.getElementById("editPhotoFootPositon");
        var footHeight=normalHeight/30 *11;
        console.log("footHeight:"+footHeight);
        foot.style.height=footHeight+"px";
 

        var head=this.$route.query.head;
        this.head=head;
        console.log(this.head);
    },
    methods:{
        back(){
            console.log("back:");
            this.$router.go(-1);
        },
        handleAvatarSuccess(file) {
            this.head = URL.createObjectURL(file.file);
            this.headFile=file.file;
        },
        //从相册中选取图片
        galleryImg(){
           let This=this;
           console.log("从相册中选择图片:"); 
           plus.gallery.pick( function(path){
               //从相册中选择图片
               console.log(path); 
               This.head=path;
               alert(path); 
               }, function ( e ) {
                   //取消选择图片 
                   console.log( "取消选择图片" ); 
                }, {filter:"image"} ); 
        },
        //调用手机摄像头并拍照
        getImage() {
            console.log("come in image:");
            let This=this;
            var cmr = plus.camera.getCamera();  
            cmr.captureImage(function(p) {  
                plus.io.resolveLocalFileSystemURL(p, function(entry) {  
                console.log("shoot success");
                // This.compressImage(entry.toLocalURL(),entry.name);  
                This.head=entry.toLocalURL();
            }, function(e) {  
                plus.nativeUI.toast("读取拍照文件错误：" + e.message);  
            });  
            }, function(e) {  
            }, {  
               filter: 'image' 
           });  
        },
        editPhoto(){
            var URL=this.IP.IP+'/user/userInfo';
            console.log(URL);
            console.log(this.headFile);
            var form=new FormData();
            form.append("head", this.headFile);
            this.$ajax({
                method:'post',
                url:URL,
                data: form,
                headers: {
                "Content-type": "multipart/form-data"
                }
            }).then(response=>{
                console.log(response.data);
                var code=response.data.code;
                if(code==0){
                    Toast(response.data.data);
                    setTimeout(()=>{
                        this.back();
                    },500)
                }else{
                    Toast(response.data.msg);
                }
            }).catch(error=>{
                console.log(error);
                //Toast(error);
            })
        }
    } 
}
</script>
<style>
.el-main,.el-header,.el-footer,.el-aside {
    padding: 0px ;
}
#editPhotoHeadPosition{
    background-color: #53cdf5;
}

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #53cdf5;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    /* width: 178px; */
    width: 100%;
    /* height: 200px; */
    /* line-height: 200px; */
    text-align: center;
  }
  .avatar {
   
    /* height: 200px; */
    padding: 0px;
    margin:0px;
    width: 100%;
    display: block;
  }
.imgCol{
    display:flex;
    align-items:center;
    justify-content:center
}
.btnPhoto{
    border:#53cdf5 1px solid;
    background-color: #fff;
    color: #53cdf5;
    font-size: 1.125em;
    width: 80%;
    height: 40px;
    outline: none;
    -webkit-border-radius: 25px;
    -moz-border-radius: 25px;
    -ms-border-radius: 25px;
    -o-border-radius: 25px;
    border-radius: 25px;
}
.backSize{
    height: 26px;
}
.titleSize{
    font-size:1.125em;
    color:#ffffff
}
@media screen and (max-width: 340px){
  .backSize{
      height: 22px;
  }
  .titleSize{
      font-size: 1em;
  }
}
@media screen and (min-width:400px){

}
@media screen and (max-width:350px){
  
}
</style>
